var Share = {};

/**
 * Ajax请求方法
 *
 * @param {Object}
 *            settings.params 参数对象，必须
 * @param {String}
 *            settings.url 请求地址，必须
 * @param {Function}
 *            settings.callback 成功后回调方法，必须
 * @param {boolean}
 *            settings.showMsg 处理成功时，是否显示提示信息 true:显示 false:不显示
 * @param {boolean}
 *            settings.showWaiting 是否显示等待条 true:显示 false:不显示
 * @param {Number}
 *            settings.timeout 超时时间
 * @param {String}
 *            settings.successMsg 成功消息
 * @param {String}
 *            settings.failureMsg 失败消息
 */
Share.AjaxRequest = function (settings) {
    // 参数对象
    var params = settings.params === undefined ? {} : settings.params,
        showWaiting = settings.showWaiting === undefined ? true : settings.showWaiting,
        showMsg = settings.showMsg === undefined ? true : settings.showMsg,
        timeout = settings.timeout === undefined ? 60 * 1000 : settings.timeout,
    // 发送请求
        waiting = null;
    if (showWaiting) {
        waiting = Ext.Msg.wait('正在处理，请稍等...', '', '');
    }
    Ext.Ajax.request({
        url: settings.url,
        params: params,
        timeout: timeout,
        success: function (response, options) {
            if (waiting != null) {
                waiting.hide();
            }
            var json = Ext.decode(response.responseText);
            if (json.success == true) {
                if (showMsg == true) { // 显示提示信息
                    // 请求成功时的提示文字
                    var successMsg = '操作成功.';
                    if (json.msg && json.msg != '') {
                        successMsg = json.msg;
                    } else if (settings.successMsg && settings.successMsg != '') {
                        successMsg = settings.successMsg;
                    }
                    Ext.Msg.alert('提示', successMsg, function () {
                        if (settings.callback) { // 回调方法
                            settings.callback(json);
                        }
                    });
                } else {
                    if (settings.callback) { // 回调方法
                        settings.callback(json);
                    }
                }
            } else if (json.success == false) {
                var message = '发生异常.';
                if (json.msg && json.msg != '') { // 后台设定的业务消息
                    message = json.msg;
                } else if (settings.failureMsg && settings.failureMsg != '') { // 前台指定的错误信息
                    message = settings.failureMsg;
                }
                if (json.exceptionMessage && json.exceptionMessage != '') { // 有异常信息
                    Share.ExceptionWindow(message, json.exceptionMessage);
                } else {
                    Ext.Msg.alert('错误', message, function () {
                        if (typeof settings.falseFun == "function") {//失败后想做的个性化操作函数
                            settings.falseFun(json);
                        }
                    });
                }
            } else if (typeof response.getResponseHeader == "function") {
                // 得到response里面的头信息
                // response.setHeader("session-status","timeout");
                var sessionStatus = response.getResponseHeader("session-status");
                if (sessionStatus == "timeout") { // timeout
                    Ext.Msg.alert('提示', '<span style="color:red"><b>登录超时,请重新登录!</b></span>', function () {
                        var win = window;
                        while (win != win.parent) {
                            win = win.parent;
                        }
                        win.location = ctx;
                    });
                    return false;
                }
                // 服务器出错
                if (sessionStatus == "pagenotfind") { // pagenotfind
                    Ext.Msg.alert('提示', '<span style="color:red"><b>很抱歉，您要访问的页面不存在！</b></span>', function () {
                    });
                    return false;
                }
                if (settings.callback) { // 回调方法
                    settings.callback(json);
                }
            }
        },
        failure: function (response, options) {
            if (waiting != null) {
                waiting.hide();
            }
            Share.ExceptionWindow('错误：' + response.status + ' ' + response.statusText, response.responseText);
        }
    });
};

/**
 * 设置系统主题
 */
Share.swapStyle = function (themeCss) {
    // Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
    // 设置主题
    var theme = themeCss === undefined ? Ext.state.Manager.get('Cxjava_theme') : themeCss;
    if (theme && theme != '') {
        // 实际改变风格样式的处理
        Ext.util.CSS.swapStyleSheet('theme', ctx + '/resources/js/extjs-4.2.1/resources/css/' + theme);
        Ext.state.Manager.set('Cxjava_theme', theme);
    } else {
        Ext.util.CSS.swapStyleSheet('theme', ctx + '/resources/js/extjs-4.2.1/resources/ext-theme-gray/ext-theme-gray-all.css');
        Ext.state.Manager.set('Cxjava_theme', 'xtheme-gray.css');
    }
};

/** 得到最顶层的window对象 */
Share.getWin = function () {
    var win = window;
    while (win != win.parent) {
        win = win.parent;
    }
    return win;
}

/** 问候 */
Share.sayHello = function () {
    var hour = new Date().getHours(),
        hello = '';
    if (hour < 6) {
        hello = '凌晨好';
    } else if (hour < 9) {
        hello = '早上好';
    } else if (hour < 12) {
        hello = '上午好';
    } else if (hour < 14) {
        hello = '中午好';
    } else if (hour < 17) {
        hello = '下午好';
    } else if (hour < 19) {
        hello = '傍晚好';
    } else if (hour < 22) {
        hello = '晚上好';
    } else {
        hello = '夜里好';
    }
    return hello + '！';
}

/**
 * 扩展基础类 判断以什么结尾
 */
String.prototype.endsWith = function (suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
/**
 * 扩展基础类 得到字符串的长度，包括中文和英文
 */
String.prototype.charlen = function () {
    var arr = this.match(/[^\x00-\xff]/ig);
    return this.length + (arr == null ? 0 : arr.length);
}
/**
 * 扩展基础类 字符串首尾去空格
 */
String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

/**
 * 扩展基础类 字符串包含字符串判断
 */
String.prototype.contains = function (sub) {
    return this.indexOf(sub) != -1;
}